<template>
  <div class='my-register'>
    <div class="logo">
        <img src="@/assets/logo.png" alt="" />
    </div>
    <div class="register-form">
      <el-form :model="ruleForm" status-icon :rules="rules" label-width="80px" ref="ruleForm">
        <el-form-item label="邮箱地址" prop="email">
          <el-input type="email" v-model="ruleForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input type="text" v-model.trim="ruleForm.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model.trim="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="重复密码" prop="rePassword">
          <el-input type="password" v-model.trim="ruleForm.rePassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-button type="primary" @click="submitForm" style="width:100%">注册</el-button>
      </el-form>
      <el-divider></el-divider>
    </div>
    <div class="register-footer">
      <p>已经有账号？ <router-link to="/login"> 点此登录</router-link></p>
    </div>
  </div>
</template>

<script>
import { registerUser } from '@/api/user'

export default {
  name: 'MyRegister',
  data () {
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('密码不能为空'))
      }
      if (!(/[\S]{6,}/.test(value))) {
        callback(new Error('密码不能小于6位'))
      }
      callback()
    }
    const validateRePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (!(/[\S]{6,}/.test(value))) {
        callback(new Error('密码不能小于6位'))
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        email: '',
        nickname: '',
        password: '',
        rePassword: ''
      },
      rules: {
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        nickname: [
          { required: true, message: '请输入用户昵称', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        rePassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validateRePass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          const res = await registerUser(this.ruleForm)
          if (res.code === 0) {
            this.$message.success(res.msg)
            this.$router.push('/login')
          } else {
            this.$message.error(res.msg)
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang='less' scoped>
.my-register {
  position: absolute;
  width: 500px;
  height: 580px;
  left: 50%;
  top: 50px;
  transform: translateX(-50%);
  background-color: #fff;
  text-align: center;
  padding: 30px 30px 30px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.2);
  border-radius: 10px;
}

.logo {
  padding: 50px 0;

  &>img {
    width: 300px;
    height: 100px;
  }
}

.register-form {
  padding: 0 20px;
}

:deep(.register-form) {
  margin-bottom: 10px;
}

.register-footer {
  text-align: center;
  font-size: 14px;
  a {
    color: #ce6e70;
  }
}

:deep(.el-form-item) {
  margin-bottom: 20px !important;
}

.el-divider--horizontal {
  margin: 12px 0;
}

:deep(.el-form-item__label) {
  text-align: left;
}
</style>
